package com.hh85.sqlite;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;

public class DataAdapter extends BaseAdapter {
   private Context context;
   private ArrayList<HashMap<String,String>> datalist ;
    public DataAdapter(Context context, ArrayList<HashMap<String,String>> datalist){
        this.context = context;
        this.datalist = datalist;
    }
    @Override
    public int getCount() {

        return datalist.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        if(view == null){
            view = LayoutInflater.from(context).inflate(R.layout.item_record,viewGroup,false);

        }

        TextView typeText = (TextView)view.findViewById(R.id.id_type);
        typeText.setText(datalist.get(i).get("type"));
        TextView priceText = (TextView)view.findViewById(R.id.id_price);
        priceText.setText("¥"+datalist.get(i).get("price"));
        TextView timeText = (TextView)view.findViewById(R.id.id_time);
        timeText.setText( datalist.get(i).get("time"));
        TextView remarkText = (TextView)view.findViewById(R.id.id_remark);
        remarkText.setText( datalist.get(i).get("remark"));

        TextView delText = (TextView)view.findViewById(R.id.id_del);
       final String id = datalist.get(i).get("_id");
        delText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DatabaseUtils   databaseUtils = new DatabaseUtils(context,"book");
                databaseUtils.open();

                databaseUtils.delete("account","_id=?",new String[]{id});
                datalist.remove(i);
                notifyDataSetChanged();
            }
        });
        return view;
    }
}
